/////////////////////////////////////////////////////////////////////////////
// Name:        MirrorlistPanel.cpp
// Purpose:     
// Author:      yospaly
// Modified by: 
// Created:     05/01/2010 11:09:27
// RCS-ID:      
// Copyright:   Copyright (C) 2008, yospaly
// Licence:     
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

////@begin includes
#include "wx/imaglist.h"
////@end includes

#include "MirrorlistPanel.h"
#include "mirrorslst.h"
#include "mounttable.h"
#include "config.h"
#include "pkgmgr.h"

////@begin XPM images
////@end XPM images


/*!
 * MirrorlistPanel type definition
 */

IMPLEMENT_DYNAMIC_CLASS( MirrorlistPanel, wxPanel )


/*!
 * MirrorlistPanel event table definition
 */

BEGIN_EVENT_TABLE( MirrorlistPanel, wxPanel )

////@begin MirrorlistPanel event table entries
    EVT_BUTTON( ID_REFRESH_MIRRORSLIST_BUTTON, MirrorlistPanel::OnREFRESHMIRRORSLISTBUTTONClick )

    EVT_TREE_SEL_CHANGED( ID_MIRRORS_TREE, MirrorlistPanel::OnMIRRORSTREESelChanged )

////@end MirrorlistPanel event table entries

END_EVENT_TABLE()


/*!
 * MirrorlistPanel constructors
 */

MirrorlistPanel::MirrorlistPanel()
{
    Init();
}

MirrorlistPanel::MirrorlistPanel( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
    Init();
    Create(parent, id, caption, pos, size, style);
}


/*!
 * MirrorlistPanel creator
 */

bool MirrorlistPanel::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
////@begin MirrorlistPanel creation
    SetExtraStyle(wxWS_EX_BLOCK_EVENTS);
    wxPanel::Create( parent, id, pos, size, style );

    CreateControls();
    Centre();
////@end MirrorlistPanel creation
    return true;
}


/*!
 * MirrorlistPanel destructor
 */

MirrorlistPanel::~MirrorlistPanel()
{
////@begin MirrorlistPanel destruction
////@end MirrorlistPanel destruction
}


/*!
 * Member initialisation
 */

void MirrorlistPanel::Init()
{
////@begin MirrorlistPanel member initialisation
    p_refresh_button = NULL;
    p_mirrors_tree = NULL;
    p_location_static = NULL;
    p_mirror_text = NULL;
////@end MirrorlistPanel member initialisation
}


/*!
 * Control creation for MirrorlistPanel
 */

void MirrorlistPanel::CreateControls()
{    
////@begin MirrorlistPanel content construction
    MirrorlistPanel* itemPanel1 = this;

    wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
    itemPanel1->SetSizer(itemBoxSizer2);

    p_refresh_button = new wxButton( itemPanel1, ID_REFRESH_MIRRORSLIST_BUTTON, _("Refresh Mirrors List"), wxDefaultPosition, wxDefaultSize, wxNO_BORDER );
    itemBoxSizer2->Add(p_refresh_button, 0, wxALIGN_LEFT|wxALL, 5);

    p_mirrors_tree = new wxTreeCtrl( itemPanel1, ID_MIRRORS_TREE, wxDefaultPosition, wxSize(240, 150), wxTR_HAS_BUTTONS |wxTR_TWIST_BUTTONS|wxTR_FULL_ROW_HIGHLIGHT|wxTR_LINES_AT_ROOT|wxTR_HIDE_ROOT|wxTR_ROW_LINES|wxTR_HAS_VARIABLE_ROW_HEIGHT|wxTR_SINGLE|wxSUNKEN_BORDER );
    itemBoxSizer2->Add(p_mirrors_tree, 1, wxGROW|wxALL, 0);

    wxStaticText* itemStaticText5 = new wxStaticText( itemPanel1, wxID_STATIC, _("Select a mirror from mirrors list or input\ncustom mirror location, ie:\n  *  http://any.web-server.com/cygwin/\n  *  ftp://any.ftp-server.com/cygwin/\n  *  (Future feature) \\\\lan-share\\cygwin\n  *  (Future feature) D:\\download\\cygwin"), wxDefaultPosition, wxDefaultSize, 0 );
    itemStaticText5->SetFont(wxFont(9, wxSWISS, wxNORMAL, wxNORMAL, false, wxT("Courier New")));
    itemBoxSizer2->Add(itemStaticText5, 0, wxGROW|wxALL, 5);

    wxBoxSizer* itemBoxSizer6 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer2->Add(itemBoxSizer6, 0, wxGROW|wxALL, 0);

    p_location_static = new wxStaticText( itemPanel1, wxID_STATIC, _("Location :"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer6->Add(p_location_static, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    p_mirror_text = new wxTextCtrl( itemPanel1, ID_MIRROR_TEXT, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer6->Add(p_mirror_text, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);

////@end MirrorlistPanel content construction

    init_tree();
}


/*!
 * Should we show tooltips?
 */

bool MirrorlistPanel::ShowToolTips()
{
    return true;
}

/*!
 * Get bitmap resources
 */

wxBitmap MirrorlistPanel::GetBitmapResource( const wxString& name )
{
    // Bitmap retrieval
////@begin MirrorlistPanel bitmap retrieval
    wxUnusedVar(name);
    return wxNullBitmap;
////@end MirrorlistPanel bitmap retrieval
}

/*!
 * Get icon resources
 */

wxIcon MirrorlistPanel::GetIconResource( const wxString& name )
{
    // Icon retrieval
////@begin MirrorlistPanel icon retrieval
    wxUnusedVar(name);
    return wxNullIcon;
////@end MirrorlistPanel icon retrieval
}


/*!
 * wxEVT_COMMAND_TREE_SEL_CHANGED event handler for ID_MIRRORS_TREE
 */

void MirrorlistPanel::OnMIRRORSTREESelChanged( wxTreeEvent& event )
{
	const wxString& name = p_mirrors_tree->GetItemText(event.GetItem());
	
	wxString url = MirrorsList::get().get_url(name);
	
	p_mirror_text->SetValue(url);
}


void MirrorlistPanel::check_reload_tree() {
    if (p_mirrors_tree->GetCount() <=1) {
        reload_tree();
    }
}

void MirrorlistPanel::reload_tree() {
    wxBeginBusyCursor();
    MirrorsList& mirrorslst = MirrorsList::get();
    mirrorslst.reload_mirrors();
    init_tree();
    wxEndBusyCursor();
}

void MirrorlistPanel::init_tree() {
	p_mirrors_tree->DeleteAllItems();
	
	MirrorsList& mirrors_lst = MirrorsList::get();
	// mirrors_lst.reload_mirrors();

	Config& config = Config::get();
	const wxString& last_mirror = config.get_last_mirror();
	
    const wxTreeItemId& root = p_mirrors_tree->AddRoot(_T("ROOT"));
    wxTreeItemId expand_item;
	
	const wxArrayString& continents = mirrors_lst.get_continents();
    for( int i=0; i<continents.GetCount(); i++ ) {
    	// add continent nodes
        const wxTreeItemId& id = p_mirrors_tree->AppendItem(root, continents[i]);
        
        const wxArrayString& countries = mirrors_lst.get_countries(continents[i]);
        
        for (int i=0; i<countries.GetCount(); i++) {
        	// add country nodes
        	const wxTreeItemId& id2 = p_mirrors_tree->AppendItem(id, countries[i]);
        	
        	const wxArrayString& mirrors = mirrors_lst.get_mirrors(countries[i]);
        	
            for (int j=0; j<mirrors.GetCount(); j++) {
            	// add mirror nodes
            	wxString& mirror = mirrors[j];
            	const wxTreeItemId& id3 = p_mirrors_tree->AppendItem(id2, mirror);
            	
            	const wxString& url = mirrors_lst.get_url(mirror);
            	
            	if ( 0==url.CmpNoCase(last_mirror) ) {
            		expand_item = id3;
            	}
        	}
    	}
    }
    
    // p_mirrors_tree->ExpandAll();
    
	
	if ( config.get_last_mirror().IsEmpty() ) {
        // TODO:
        // p_cancel_button->Disable(); // mirror location is a mandatory
	}
	
	// p_mirror_text->SetFocus();
	// p_mirror_text->SetInsertionPointEnd();
	
	if ( expand_item.IsOk() && !last_mirror.IsEmpty() ) {
		p_mirror_text->SetValue(last_mirror);
		
		const wxTreeItemId& id10 = p_mirrors_tree->GetItemParent(expand_item);
		const wxTreeItemId& id9 = p_mirrors_tree->GetItemParent(id10);
		
		// p_mirrors_tree->Expand(id9);
		//p_mirrors_tree->Expand(id10);
		//p_mirrors_tree->Expand(*expand_item);
		p_mirrors_tree->SelectItem(expand_item);
		p_mirrors_tree->SetFocus();
	}
}

/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_REFRESH_MIRRORSLIST_BUTTON
 */

void MirrorlistPanel::OnREFRESHMIRRORSLISTBUTTONClick( wxCommandEvent& event )
{
    event.Skip();
    reload_tree();
}

